package com.cai310.lottery.ticket.protocol.local.utils;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;

import com.cai310.lottery.Constant;
import com.cai310.lottery.PlConstant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.support.ContentBean;
import com.cai310.lottery.support.dlt.DltCompoundContent;
import com.cai310.lottery.support.dlt.DltContentBeanBuilder;
import com.cai310.lottery.support.dlt.DltPlayType;
import com.cai310.lottery.support.pl.PlPlayType;

public  class DLTCPUtil extends CPUtil{
	/**
	 *  
	 * @param lottery
	 * @param playType
	 * @return
	 */
	private static final String NUMCODE = ",";
	private static final String CONTENTCONNECTCODE = "-";
	private static final String DANSPLITCODE = ":";
	private static final String SINGSPILTCODE = "+";
	private static final NumberFormat TWO_NF = new DecimalFormat("00");
	@Override
	public String getCpLotteryId(Lottery lottery,Byte betType){
		return "8";
	}
	@Override
	public String getCpBetType(Ticket ticket){
		Byte betType = ticket.getBetType();
		return ""+betType;
	}
	@Override
	public String getCpPlayType(Ticket ticket){
		///extension格式是    是否追加-playType  
		String extension = ticket.getExtension();
		String[] arr = extension.split("-");
		return arr[1].trim();
		
	}
	@Override
	public String getBetContent(Ticket ticket){
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return ticket.getContent();
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return ticket.getContent();
		}
		return null;
	}
	protected String formatBetNum(String line,NumberFormat nf){
		StringBuffer sb = new StringBuffer();
		String[] arr = line.split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		for (int i = 0; i < arr.length; i++) {
			sb.append(nf.format(Integer.valueOf(arr[i]))).append(NUMCODE);
			if(i==4){
				sb.append(CONTENTCONNECTCODE);
			}
		}
		return sb.substring(0,sb.length()-NUMCODE.length());
	}
	@Override
	public String getSpecialFlag(Ticket ticket) {
		///extension格式是    是否追加-playType  
		String extension = ticket.getExtension();
		String[] arr = extension.split("-");
		if("1".equals(arr[0])){
			return "1";
		}else{
			return "";
		}
	}
	public static SalesMode getSalesMode(Ticket ticket) throws DataException{
		String extension = ticket.getExtension();
		String[] arr = extension.split("-");
		String playTypeStr = arr[1];
		if("1".equals(playTypeStr)){
			  return SalesMode.SINGLE;
		}else if("2".equals(playTypeStr)){
            return SalesMode.COMPOUND;
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	public static DltPlayType getPlayType(Ticket ticket) throws DataException{
		String extension = ticket.getExtension();
		String[] arr = extension.split("-");
		String specialFlag = "";
		if("1".equals(arr[0])){
			specialFlag = "1";
		}
		DltPlayType dltPlayType;
		Byte betType = ticket.getBetType();
		if(betType.equals(Byte.valueOf("1"))){//12选2
		    dltPlayType = DltPlayType.Select12to2;
	    }else if(betType.equals(Byte.valueOf("0"))){
	    	dltPlayType = DltPlayType.General;
	    	if("1".equals(specialFlag)){
	    		dltPlayType = DltPlayType.GeneralAdditional;
	    	}
	    }else{
			//报错
			throw new DataException("组合出票玩法出错");
		}
		return dltPlayType;
	}
	public static ContentBean buildContentBean(Ticket ticket) throws DataException{
		String extension = ticket.getExtension();
		String[] arr = extension.split("-");
		String playTypeStr = arr[1].trim();
		String specialFlag = "";
		Byte betType = ticket.getBetType();
		if("1".equals(arr[0])){
			specialFlag = "1";
		}
		DltPlayType dltPlayType;
		if(betType.equals(Byte.valueOf("1"))){//12选2
		    dltPlayType = DltPlayType.Select12to2;
	    }else if(betType.equals(Byte.valueOf("0"))){
	    	dltPlayType = DltPlayType.General;
	    	if("1".equals(specialFlag)){
	    		dltPlayType = DltPlayType.GeneralAdditional;
	    	}
	    }else{
			//报错
			throw new DataException("组合出票玩法出错");
		}
		if("1".equals(playTypeStr)){
			return DltContentBeanBuilder.buildSingleContentBean(buildSingleStr(ticket.getContent()), dltPlayType);
		}else if("2".equals(playTypeStr)){
				if(ticket.getContent().indexOf(DANSPLITCODE)!=-1){
					return DltContentBeanBuilder.buildCompoundContentBean(buildCompoundStr(ticket.getContent(),ticket.getUnits()), dltPlayType);
				}else{
					//报错
					throw new DataException("组合出票内容出错");
				}
		}else{
			//报错
			throw new DataException("组合出票投注玩法出错");
		}
	}
	protected static String buildCompoundStr(String content,Integer units){
		
		String[] arr = content.split(DANSPLITCODE);
		StringBuffer sb = new StringBuffer();
		sb.append(units+":");
		if(StringUtils.isNotBlank(arr[0])){
			sb.append(arr[0]+";"+arr[1]);
		}else{
			sb.append(arr[1]);
		}
		sb.append("|");
		if(StringUtils.isNotBlank(arr[2])){
			sb.append(arr[2]+";"+arr[3]);
		}else{
			sb.append(arr[3]);
		}
		return sb.toString();
	}
	protected static String buildSingleStr(String content){
		content = content.replaceAll("\\"+SINGSPILTCODE, "\r\n");
		content = content.replaceAll(NUMCODE, Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		content = content.replaceAll(CONTENTCONNECTCODE, Constant.SINGLE_SEPARATOR_FOR_NUMBER);
		return content;
		
	}
	@Override
	public String getUpdatePeriodNumber(Ticket ticket) {
		return null;
	}
	public static Collection<DltCompoundContent> getCompoundContent(Ticket ticket) throws DataException {
		Collection<DltCompoundContent> c = JSONArray.toCollection(JSONArray.fromObject(buildContentBean(ticket).getContent()),
				DltCompoundContent.class);
		return c;
	}
}
